mini subroutines
Selected Subroutines
A routine shown without a prefix is built-in. A routine shown with a prefix requires an include file. The prefix is optional (unless there is a name clash) but is shown as a reminder that an include file is needed. For example: if you write include std/console.e then you can use console:display or just display in a statement.
For examples of their usage see sanity.ex or the other Euphoria files. Complete details are available in the Euphoria Reference Manual.
To indicate what may be passed in and returned the following prefixes are used:
x -- a general object (sequence or atom) a -- an atom s -- a sequence i -- an integer fn -- an integer file number st -- a string sequence or single-character atom
Types:
i = integer(x) -- is x an integer? i = atom(x) -- is x an atom? i = sequence(x) -- is x a sequence? i = object(x) -- is x initialized?
Operations on Objects:
i = length(x) -- length of sequence s = repeat(x, a) -- repeat x a times s2 = append(x1, x) -- append x to end of s1 s2 = prepend(x1, x) -- prepend x at beginning of s1
Searching and Sorting:
i = compare(x1, x2) -- compare x1 to x2, return +1, -1 or 0 i = find(x, s) -- find position of x as an item within s i = match(s1, s2) -- find position of s1 as a slice within s2 x2 = sort:sort(s) -- sort a sequence of arbitrary objects -- [include std/sort.e ]
File I/O:
fn = open(st1, st2) -- open file name st1 with mode st2 e.g. "r"
Some files are opened automatically for you:
0 -- standard input 1 -- standard output 2 -- standard error close(fn) -- close file number fn print(fn, x) -- print value of x to file fn printf(fn, st, x) -- formatted print. st contains %d, %f, %s etc.
x is a sequence of values to be formatted
puts(fn, st) -- output string st to file fn i = getc(fn) -- get next character from file fn (-1 is EOF) x = gets(fn) -- get next line from file fn (-1 is EOF) i = get_key() -- get key pressed, or return -1 s = get(fn) -- read next Euphoria object representation from -- file fn [include get.e and see comments in get.e]
Operating System
a = time() -- time in seconds since a fixed point s = date() -- {year, month, day, hour, minute, second, day of week, day of year} s = command_line() -- sequence of words from ex command line system(s, i) -- execute a operating system command s, i = 0 => restore graphics mode i = 1 => beep, wait for key press, then restore graphics mode i = 2 => don't restore graphics mode s2 = getenv(s1) -- return value of an environment variable
Debugging:
trace(a) -- turn tracing on (1) or off (0)
Console or Terminal:
While you can write graphical programs with Euphoria, this mini manual only shows a few text mode routines:
clear_screen() -- clear the screen console:position(a1, a2) -- move cursor to line a1, column a2 -- [include std/console.e]
for the following, include std/graphics.e and read the comments and code in graphics.e for more details
graphcst: = video_config() -- {color monitor?, mode, rows, columns, -- xpixels, ypixels, #colors} graphics:scroll(i) -- scroll screen up or down graphics:wrap(i) -- wrap or not at right margin graphics:text_color(i) -- foreground text color graphics:bk_color(i) -- background text or graphics color
Math:
x2 = sqrt(x1) -- square root x2 = rand(x1) -- random number x2 = sin(x1) -- trig function x2 = cos(x1) -- trig function x2 = tan(x1) -- trig function x2 = log(x1) -- natural log x2 = floor(x1) -- round down to an integer x2 = remainder(x1, x2) -- remainder when x1 is divided by x2 x2 = power(x1, x2) -- x1 to the power x2